package com.kn.order.service.impl;

import com.kn.common.model.R;
import com.kn.order.mapper.ExtraMapper;
import com.kn.order.model.Extra;
import com.kn.order.model.ExtraExample;
import com.kn.order.service.ExtraService;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
@RequiredArgsConstructor
public class ExtraServiceImpl implements ExtraService {
    private final ExtraMapper extraMapper;
    private static final Logger LOGGER= LoggerFactory.getLogger(ExtraService.class);
  /*   @Override
    public R add(Extra extra) {
        int i = extraMapper.insertSelective(extra);
        return R.ok();
    }
   @Override
    public R delete(Long id) {
        Extra extra = new Extra();
        extra.setId(id);
        extra.setIsdel(true);
        extra.setOptime(new Date());
        ExtraExample extraExample =new ExtraExample();
        extraExample.createCriteria()
                .andIdEqualTo(id);
        extraMapper.updateByExampleSelective(extra,extraExample);
        return R.ok();
    }

    private boolean isRepeat(Extra extra) {
        ExtraExample example = new ExtraExample();
        ExtraExample.Criteria criteria = example.createCriteria()
                .andSummaryEqualTo(extra.getSummary())
                .andIsdelEqualTo(false);
        if (extra.getId() != null) {
            criteria.andIdNotEqualTo(extra.getId());
        }
        List<Extra> extras = extraMapper.selectByExample(example);
        if (extras.size() == 0) {
            return false;
        }
        return true;
    }

    @Override
    public R save(MultipartFile file, Extra extra) {
        if (isRepeat(extra)) {
            return R.error("附件说明不能重复！");
        }
        extra.setOptime(new Date());
        int r = getR(file, extra);
        if (r == 2) {
            return R.error("系统错误！");
        }
        if (extra.getId() == null) {
            if (r == -1) {
                return R.error("附件不能为空！");
            }
            extraMapper.insertSelective(extra);
        } else {
            ExtraExample example =new ExtraExample();
            example.createCriteria().andIdEqualTo(extra.getId());
            extraMapper.updateByExampleSelective(extra,example);
        }
        return R.ok();
    }

    private int getR(MultipartFile file, Extra extra) {
        if (file.isEmpty()) {
            return -1;
        }
        String fileName = file.getOriginalFilename();
        extra.setFilename(fileName);
        try {
            extra.setExtra(file.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
            return 2;
        }
        return 3;
    }

    @Override
    public List<Extra> listall(Long orderid) {
        ExtraExample example = new ExtraExample();
        example.createCriteria()
                .andOrderIdEqualTo(orderid)
                .andIsdelEqualTo(false);
        return extraMapper.selectByExample(example);
    }

    @Override
    public R downloadFile(HttpServletResponse response, Long id) {
        ExtraExample example = new ExtraExample();
        example.createCriteria()
                .andIdEqualTo(id);
        List<Extra> extras = extraMapper.selectByExampleWithBLOBs(example);
        if(extras==null||extras.size()<1){
            return R.error("下载失败1");
        }
        Extra extra = extras.get(0);
        response.setContentType("application/force-download");// 设置强制下载不打开
        response.addHeader("Content-Disposition", "attachment;fileName=" + extra.getFilename());
        try{
            ServletOutputStream outputStream = response.getOutputStream();
            outputStream.write(extra.getExtra());
        }catch (IOException e){
            e.printStackTrace();
            return R.error("下载失败");
        }
        return R.ok();
    }
  */

}
